```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java Bean生命周期注解详解 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background: #2d2d2d;
            border-left: 4px solid #667eea;
        }
        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .footer-link:hover {
            color: #a5b4fc;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-6 font-serif">Java Bean 生命周期管理</h1>
            <p class="text-xl md:text-2xl mb-8 font-light opacity-90">深入理解 <span class="font-bold">@PostConstruct</span> 和 <span class="font-bold">@PreDestroy</span> 注解</p>
            <div class="flex justify-center space-x-4">
                <a href="#postconstruct" class="px-6 py-3 bg-white text-indigo-700 font-medium rounded-lg hover:bg-gray-100 transition duration-300">
                    <i class="fas fa-play-circle mr-2"></i>@PostConstruct
                </a>
                <a href="#predestroy" class="px-6 py-3 bg-indigo-800 text-white font-medium rounded-lg hover:bg-indigo-900 transition duration-300">
                    <i class="fas fa-stop-circle mr-2"></i>@PreDestroy
                </a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <p class="text-lg text-gray-700 mb-6 leading-relaxed">
                在 Java 企业级开发中，特别是使用 Spring 框架时，<code class="bg-gray-100 px-2 py-1 rounded">@PostConstruct</code> 和 <code class="bg-gray-100 px-2 py-1 rounded">@PreDestroy</code> 
                是两个至关重要的生命周期注解。它们为开发者提供了一种优雅的方式来控制 Bean 的初始化和销毁过程，确保资源得到正确管理。
            </p>
            
            <div class="bg-blue-50 border-l-4 border-blue-500 p-4 mb-8 rounded-r">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-info-circle text-blue-500 text-xl"></i>
                    </div>
                    <div class="ml-3">
                        <h3 class="text-sm font-medium text-blue-800">关键概念</h3>
                        <div class="mt-2 text-sm text-blue-700">
                            <p>
                                生命周期注解允许开发者在 Bean 生命周期的特定阶段注入自定义逻辑，而不需要显式实现 Spring 接口或使用 XML 配置。
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization -->
        <section class="mb-16">
            <h2 class="text-2xl font-bold text-gray-800 mb-6 font-serif">生命周期流程可视化</h2>
            <div class="bg-white rounded-xl shadow-md overflow-hidden p-6">
                <div class="mermaid">
                    sequenceDiagram
                        participant Container as Spring容器
                        participant Bean as Bean实例
                        Container->>Bean: 1. 实例化Bean
                        Container->>Bean: 2. 依赖注入
                        Bean-->>Container: 3. @PostConstruct方法执行
                        Container->>Bean: 4. Bean就绪可用
                        Container->>Bean: 5. @PreDestroy方法执行
                        Container->>Bean: 6. 销毁Bean
                </div>
                <p class="mt-4 text-gray-600 text-sm">
                    上图展示了 Spring Bean 从创建到销毁的完整生命周期，其中 <code>@PostConstruct</code> 和 <code>@PreDestroy</code> 分别在特定阶段被调用。
                </p>
            </div>
        </section>

        <!-- @PostConstruct Section -->
        <section id="postconstruct" class="mb-16 scroll-mt-16">
            <div class="flex items-center mb-6">
                <div class="bg-green-500 p-2 rounded-lg mr-4">
                    <i class="fas fa-play-circle text-white text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800 font-serif">@PostConstruct</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="bg-white rounded-xl shadow-md overflow-hidden feature-card transition duration-300">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-green-100 p-2 rounded-full mr-3">
                                <i class="fas fa-check-circle text-green-600"></i>
                            </div>
                            <h3 class="text-lg font-medium text-gray-800">核心用途</h3>
                        </div>
                        <p class="text-gray-600">
                            <code>@PostConstruct</code> 注解标记的方法会在依赖注入完成后、Bean 初始化阶段之后自动调用。这是执行初始化任务的理想时机。
                        </p>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md overflow-hidden feature-card transition duration-300">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-blue-100 p-2 rounded-full mr-3">
                                <i class="fas fa-laptop-code text-blue-600"></i>
                            </div>
                            <h3 class="text-lg font-medium text-gray-800">典型应用场景</h3>
                        </div>
                        <ul class="text-gray-600 space-y-2">
                            <li><i class="fas fa-check text-green-500 mr-2"></i>加载配置信息</li>
                            <li><i class="fas fa-check text-green-500 mr-2"></i>初始化资源</li>
                            <li><i class="fas fa-check text-green-500 mr-2"></i>检查依赖关系</li>
                            <li><i class="fas fa-check text-green-500 mr-2"></i>执行启动时验证</li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <h3 class="text-xl font-semibold text-gray-800 mb-4 font-serif">代码示例</h3>
            <div class="code-block rounded-lg overflow-hidden mb-6">
                <div class="flex items-center bg-gray-800 px-4 py-2">
                    <div class="flex space-x-2">
                        <div class="w-3 h-3 rounded-full bg-red-500"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500"></div>
                    </div>
                    <div class="ml-4 text-sm text-gray-400">
                        MyBean.java
                    </div>
                </div>
                <pre class="text-gray-200 p-4 overflow-x-auto"><code>import javax.annotation.PostConstruct;

@Component
public class MyBean {

    @PostConstruct
    public void init() {
        // 在依赖注入完成后执行初始化操作
        System.out.println("Bean 初始化完成");
        // 这里可以添加自定义初始化逻辑
        initializeResources();
        validateConfiguration();
    }
    
    private void initializeResources() {
        // 初始化资源
    }
    
    private void validateConfiguration() {
        // 验证配置
    }
}</code></pre>
            </div>
            
            <div class="bg-yellow-50 border-l-4 border-yellow-400 p-4 rounded-r">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-exclamation-triangle text-yellow-500 text-xl"></i>
                    </div>
                    <div class="ml-3">
                        <h3 class="text-sm font-medium text-yellow-800">最佳实践</h3>
                        <div class="mt-2 text-sm text-yellow-700">
                            <ul class="list-disc pl-5 space-y-1">
                                <li>保持 <code>@PostConstruct</code> 方法简洁，避免耗时操作</li>
                                <li>方法可以有任何名称，但应该返回 void 且不带参数</li>
                                <li>考虑将复杂初始化逻辑分解到私有方法中</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- @PreDestroy Section -->
        <section id="predestroy" class="mb-16 scroll-mt-16">
            <div class="flex items-center mb-6">
                <div class="bg-red-500 p-2 rounded-lg mr-4">
                    <i class="fas fa-stop-circle text-white text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800 font-serif">@PreDestroy</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="bg-white rounded-xl shadow-md overflow-hidden feature-card transition duration-300">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-red-100 p-2 rounded-full mr-3">
                                <i class="fas fa-power-off text-red-600"></i>
                            </div>
                            <h3 class="text-lg font-medium text-gray-800">核心用途</h3>
                        </div>
                        <p class="text-gray-600">
                            <code>@PreDestroy</code> 注解标记的方法会在 Bean 被容器销毁之前自动调用。这是执行清理操作的理想时机。
                        </p>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md overflow-hidden feature-card transition duration-300">
                    <div class="p-6">
                        <div class="flex items-center mb-4">
                            <div class="bg-purple-100 p-2 rounded-full mr-3">
                                <i class="fas fa-broom text-purple-600"></i>
                            </div>
                            <h3 class="text-lg font-medium text-gray-800">典型应用场景</h3>
                        </div>
                        <ul class="text-gray-600 space-y-2">
                            <li><i class="fas fa-check text-green-500 mr-2"></i>释放资源</li>
                            <li><i class="fas fa-check text-green-500 mr-2"></i>关闭连接</li>
                            <li><i class="fas fa-check text-green-500 mr-2"></i>保存状态</li>
                            <li><i class="fas fa-check text-green-500 mr-2"></i>执行日志记录</li>
                        </ul>
                    </div>
                </div>
            </div>
            
            <h3 class="text-xl font-semibold text-gray-800 mb-4 font-serif">代码示例</h3>
            <div class="code-block rounded-lg overflow-hidden mb-6">
                <div class="flex items-center bg-gray-800 px-4 py-2">
                    <div class="flex space-x-2">
                        <div class="w-3 h-3 rounded-full bg-red-500"></div>
                        <div class="w-3 h-3 rounded-full bg-yellow-500"></div>
                        <div class="w-3 h-3 rounded-full bg-green-500"></div>
                    </div>
                    <div class="ml-4 text-sm text-gray-400">
                        MyResourceBean.java
                    </div>
                </div>
                <pre class="text-gray-200 p-4 overflow-x-auto"><code>import javax.annotation.PreDestroy;

@Component
public class MyResourceBean {

    private Connection connection;
    
    @PostConstruct
    public void init() {
        // 初始化数据库连接
        this.connection = createConnection();
    }
    
    @PreDestroy
    public void cleanup() {
        // 在 Bean 被销毁之前执行清理操作
        System.out.println("Bean 即将被销毁");
        
        // 关闭数据库连接
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
                System.err.println("关闭连接时出错: " + e.getMessage());
            }
        }
    }
    
    private Connection createConnection() {
        // 创建连接的具体实现
        return null;
    }
}</code></pre>
            </div>
            
            <div class="bg-blue-50 border-l-4 border-blue-500 p-4 rounded-r">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-lightbulb text-blue-500 text-xl"></i>
                    </div>
                    <div class="ml-3">
                        <h3 class="text-sm font-medium text-blue-800">应用建议</h3>
                        <div class="mt-2 text-sm text-blue-700">
                            <ul class="list-disc pl-5 space-y-1">
                                <li>确保 <code>@PreDestroy</code> 方法是幂等的，可以安全地多次调用</li>
                                <li>处理所有可能的异常情况，避免异常中断清理流程</li>
                                <li>对于关键资源，考虑添加日志记录以确认清理操作成功</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Comparison Section -->
        <section class="mb-16">
            <h2 class="text-2xl font-bold text-gray-800 mb-8 font-serif">注解对比</h2>
            <div class="bg-white rounded-xl shadow-md overflow-hidden">
                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">特性</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">@PostConstruct</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">@PreDestroy</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">调用时机</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">依赖注入完成后，Bean初始化阶段之后</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">Bean被容器销毁之前</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">主要用途</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">初始化操作</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">清理操作</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">方法要求</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">void，无参数</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">void，无参数</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">包位置</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">javax.annotation</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">javax.annotation</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">执行次数</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">每个Bean生命周期一次</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">每个Bean生命周期一次</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </section>

        <!-- Additional Tips -->
        <section class="mb-16">
            <h2 class="text-2xl font-bold text-gray-800 mb-6 font-serif">进阶提示</h2>
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
                        <i class="fas fa-random text-purple-500 mr-3"></i>执行顺序
                    </h3>
                    <p class="text-gray-600 mb-4">
                        当多个 Bean 之间存在依赖关系时，<code>@PostConstruct</code> 方法的执行顺序遵循依赖关系。被依赖的 Bean 会先初始化。
                    </p>
                    <p class="text-gray-600">
                        类似地，<code>@PreDestroy</code> 方法的执行顺序与 <code>@PostConstruct</code> 相反，依赖其他 Bean 的会先被销毁。
                    </p>
                </div>
                <div class="bg-white p-6 rounded-xl shadow-md">
                    <h3 class="text-xl font-semibold text-gray-800 mb-4 flex items-center">
                        <i class="fas fa-exclamation-circle text-orange-500 mr-3"></i>注意事项
                    </h3>
                    <ul class="text-gray-600 space-y-2">
                        <li><i class="fas fa-angle-right text-gray-400 mr-2"></i>这些注解不是 Spring 特有的，而是 Java EE 标准的一部分</li>
                        <li><i class="fas fa-angle-right text-gray-400 mr-2"></i>在原型(prototype)作用域的 Bean 上，<code>@PreDestroy</code> 不会被调用</li>
                        <li><i class="fas fa-angle-right text-gray-400 mr-2"></i>方法不应抛出异常，否则可能导致初始化失败</li>
                    </ul>
                </div>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-lg font-medium text-white">技术小馆</h3>
                    <p class="mt-1 text-sm">探索技术的无限可能</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="footer-link text-sm hover:text-white transition duration-300">
                        <i class="fas fa-globe mr-2"></i>http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="mt-8 pt-8 border-t border-gray-800 text-center text-sm">
                <p>© 2023 技术小馆. 保留所有权利.</p>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: {
                useMaxWidth: false,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```